package com.um.jdy.app.shop.manager;


import cn.hutool.core.date.DateUtil;
import com.um.jdy.app.shop.entity.dto.GoodsCollectionPageDTO;
import com.um.jdy.app.shop.entity.dto.GoodsIdDTO;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.jdy.common.shop.entity.po.GoodsCollection;
import com.um.jdy.common.shop.entity.vo.GoodsVO;
import com.um.jdy.common.shop.service.GoodsCollectionService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class GoodsCollectionManager {

    @Autowired
    GoodsCollectionService goodsCollectionService;

    @Autowired
    MemberUserService memberUserService;

    public Page<GoodsVO> getPage(GoodsCollectionPageDTO dto){
        Page page = new Page(dto);
        return goodsCollectionService.getViewModelPageBySqlName(page,
                "get_collection_goods_page", "get_collection_goods_count",
                GoodsVO.class, new String[]{memberUserService.get().getMemberUserId(),memberUserService.getComId(),
                        StringUtil.getQueryParam(dto.getShop_goods_name())});
    }

    /**
     * 收藏/取消收藏
     * @return
     */
    public void collection(GoodsIdDTO dto){
        String userId = memberUserService.get().getMemberUserId();

        GoodsCollection entity = goodsCollectionService.getEntity(new String[]{"member_user_id", "shop_goods_id"},
                new String[]{userId, dto.getShopGoodsId()});
        if (null == entity) {
            entity = new GoodsCollection();
            entity.setMemberUserId(userId);
            entity.setShopGoodsId(dto.getShopGoodsId());
            entity.setCreateTime(DateUtil.date());
            entity.setIsCollection(YesOrNo.Yes.name());
            goodsCollectionService.save(entity);
            return;
        }

        if(YesOrNo.Yes.name().equals(entity.getIsCollection())){
            entity.setIsCollection(YesOrNo.No.name());
        }else{
            entity.setCreateTime(DateUtil.date());
            entity.setIsCollection(YesOrNo.Yes.name());
        }
        goodsCollectionService.save(entity);
    }

}
